package com.woniuxy.jdkproxy;


import com.woniuxy.aspect.MyAspect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxyFactory {
    public static Object getProxyObject(final Object targetClass){
        return Proxy.newProxyInstance(targetClass.getClass().getClassLoader(), targetClass.getClass().getInterfaces(),
                new InvocationHandler(){
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        MyAspect myAspect = new MyAspect();
                        myAspect.beforeLog();

                        Object invoke = method.invoke(targetClass, args);
                        myAspect.afterLog();
                        return invoke;
                    }
                });
    }
}
